package com.kewo.listviewreturnfirst;

import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;

import com.example.library.BaseRecycleViewAdapter;
import com.example.library.RefreshAndLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity{

    RecyclerView mRecyclerView;
    FloatingActionButton mActionButton;
    boolean mbIsVisible = false;
    private Context mContext;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private MainAdapter mainAdapter;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mainAdapter.finishLode(BaseRecycleViewAdapter.Status.STATUS_NOMAL);
            mainAdapter.finishRefresh();
            if (msg.what == 0) {
                mainAdapter.addLoadMoreItmes(initData("加载更多"));
            }
            if (msg.what == 1) {
                mainAdapter.addRefreshItmes(initData("刷新"));
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        initView();
        initdata();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.my_listView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mActionButton = (FloatingActionButton) findViewById(R.id.floatingBtn);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mainAdapter = new MainAdapter(this, mRecyclerView, mSwipeRefreshLayout);
        mRecyclerView.setAdapter(mainAdapter);
        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        mainAdapter.setOnRefreshAndLoadMoreListener(new RefreshAndLoadMoreListener() {
            @Override
            public void onLoadingMoreData() {
                handler.sendEmptyMessageDelayed(0, 3000);

            }

            @Override
            public void onRefreshingData() {
                handler.sendEmptyMessageDelayed(1, 3000);
            }
        });
    }
    private ArrayList<String> initData(String data) {
        ArrayList<String> list = new ArrayList<>();
        for (char i = 'A'; i <= 'Z'; i++) {
            list.add(String.valueOf(i) + data);
        }
        return list;
    }
    private void initdata() {

        mActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRecyclerView.smoothScrollToPosition(0);
            }
        });
        mainAdapter.addRefreshItmes(initData("数据"));
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.e("123456", "onScrolled====="+dx+"   "+dy );
                if (!mRecyclerView.canScrollVertically(1)) {
                    mActionButton.setVisibility(View.GONE);
                }
                if (mRecyclerView.canScrollVertically(-1)) {//还能向上滚动
                    mActionButton.setVisibility(View.VISIBLE);
                    if (mbIsVisible){

                    }else {
                        ValueAnimator valueAnimatorBar = ValueAnimator.ofInt(0, dip2px(mContext, 80));
                        valueAnimatorBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                                //获取当前的height值
                                int h = (Integer) valueAnimator.getAnimatedValue();
                                //动态更新view的高度
                                mActionButton.getLayoutParams().height = h;
                                mActionButton.getLayoutParams().width = h;

                                mActionButton.requestLayout();
                            }
                        });
                        valueAnimatorBar.setDuration(300);
                        //开始动画
                        valueAnimatorBar.start();
                        mbIsVisible=!mbIsVisible;
                    }

                }else {
                    if (mbIsVisible){
                        ValueAnimator valueAnimatorBar = ValueAnimator.ofInt(dip2px(mContext, 80), 0);
                        valueAnimatorBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                                int h = (Integer) valueAnimator.getAnimatedValue();
                                mActionButton.getLayoutParams().height = h;
                                mActionButton.getLayoutParams().width = h;

                                mActionButton.requestLayout();
                            }
                        });
                        valueAnimatorBar.setDuration(300);
                        //开始动画
                        valueAnimatorBar.start();
                        mbIsVisible=!mbIsVisible;
                    }else {

                    }

                }
            }
        });
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
